package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.ExpenditureItems;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IExpenditureItemsService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//支出明细
@Controller
@RequestMapping("expenditureItems")
public class ExpenditureItemsController {

    @Autowired
    private IExpenditureItemsService expenditureItemsService;

    @RequiresPermissions("expenditureItems:index")
    @PermissionName("支出明细首页")
    @RequestMapping("")
    public String index() {
        expenditureItemsService.list();
        return "expenditureItems/list";
    }

    //支出明细信息列表/带分页
    @RequiresPermissions("expenditureItems:list")
    @PermissionName("支出明细列表")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo) {
        return expenditureItemsService.query(qo);
    }


    //所有支出明细信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        return expenditureItemsService.list();
    }

    //支出分析
    @RequestMapping("getAnalysis")
    @ResponseBody
    public Object getAnalysis() {
        return expenditureItemsService.getAnalysis();
    }


    //支出明细添加
    @RequiresPermissions("expenditureItems:add")
    @PermissionName("支出明细添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(ExpenditureItems entity) {
        JSONResult result = new JSONResult();
        try {
            expenditureItemsService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //支出明细编辑
    @RequiresPermissions("expenditureItems:edit")
    @PermissionName("支出明细编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(ExpenditureItems entity) {
        JSONResult result = new JSONResult();
        try {
            expenditureItemsService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    @RequestMapping("getTotalAmount")
    @ResponseBody
    public Object getTotalAmount() {
        return expenditureItemsService.getTotalAmount();
    }

    //支出明细删除
    @RequiresPermissions("expenditureItems:delete")
    @PermissionName("支出明细删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            expenditureItemsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }
}
